//
//  ViewController.m
//  MLFMDB
//
//  Created by 马文星 on 16/5/11.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "ViewController.h"
#import "DAO.h"
#import "UserDTO.h"
#import "BookDTO.h"
#import "UserDAO.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageVIew;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [DAO creatTableIfNeed];

}
- (IBAction)insert:(id)sender {
    
    NSArray *arrr = @[@"ma",@"wen",@"xing"];
    
    NSDictionary *dic = @{@"key":@"mawenxing"};
    
    BookDTO *bDto = [BookDTO bookWithBookName:@"JinPingMei" bookId:1111];
    
    UIImage *imag = [UIImage imageNamed:@"1"];
    
    NSLog(@"date = %@",[NSDate date]);
    //2016-05-11 10:04:50 +0000
    //2016-05-11 10:05:40 +0000  13
    //2016-05-11 10:06:01 +0000    31
    
    UserDTO *dto = [UserDTO userWithUserName:@"mawenxing" userAge:18 userScore:11.1 arr:arrr dic:dic book:bDto date:[NSDate date] image:imag];
    
    if ([[UserDAO sharedInstance] insertUserDTO:dto]) {
    
        NSLog(@"插入成功");
    }else{
        
        NSLog(@"插入失败");
    }
    
    
}
- (IBAction)load:(id)sender {
    
    
  NSArray *arr =  [[UserDAO sharedInstance] loadUserData];
    
    NSLog(@"获取的数组 = %@",arr);
    
    if (arr.count>0) {
        
        UserDTO *dto = arr[0];
        
        self.imageVIew.image = dto.img;
    }else{
    
        
        NSLog(@"没有取到值，请查看数据库是否有年龄为18的");
    }
}
- (IBAction)update:(id)sender {
    
    UserDTO *dto = [[UserDTO alloc] init];
    
    if ([[UserDAO sharedInstance] updateUserDTO:dto]) {
    
        NSLog(@"更新成功");
    }else{
    
        NSLog(@"更新失败");
    }
}

- (IBAction)delete:(id)sender {
    
    UserDTO *dto = [[UserDTO alloc] init];
    
    if ([[UserDAO sharedInstance] deleteUserDTO:dto]) {
        
        NSLog(@"删除成功");
    }else{
        
        NSLog(@"删除失败");
    }
}

@end
